//
//  setStartAndEndPointView.m
//  sdk2.0zhengquandasha
//
//  Created by Choi on 15/12/23.
//  Copyright © 2015年 palmaplus. All rights reserved.
//

#import "setStartAndEndPointView.h"
@interface setStartAndEndPointView ()<UIAlertViewDelegate>
@property (nonatomic,strong)UIButton *btnStart;
@property (nonatomic,strong)UIButton *btnEnd;
@property (nonatomic,assign)CGPoint currentPoint;
@property (nonatomic,strong)UIImageView *startImage;
@property (nonatomic,strong)UIImageView *endImage;
@end
@implementation setStartAndEndPointView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initAll];
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.layer.borderWidth = 1;
        self.layer.cornerRadius = 5;
    }
    return self;
}

-(void)initAll{
    CGSize imageSize = CGSizeMake(30, 30);
    CGFloat gap = 30;
    CGSize size = self.frame.size;
    UILabel *labLine = [[UILabel alloc]initWithFrame:CGRectMake(size.width / 2, 0, 1, size.height)];
    labLine.backgroundColor = [UIColor grayColor];
    [self addSubview:labLine];
    
    

    _startImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ico_arrow"]];
    _startImage.frame = CGRectMake(gap, (size.height - imageSize.height)/2, imageSize.width, imageSize.height);
    [self addSubview:_startImage];
    
    _btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnStart.frame = CGRectMake(CGRectGetMaxX(_startImage.frame)- 25, 0, size.width/2 - imageSize.width , size.height);
    //_btnStart.frame = CGRectMake(0, 0, size.width/2 , size.height);
//    _btnStart.layer.borderWidth = 1;
//    _btnStart.layer.borderColor = [UIColor grayColor].CGColor;
    
    [_btnStart setTitle:@"设置起点" forState:UIControlStateNormal];
    _btnStart.titleLabel.font = [UIFont systemFontOfSize:20];
    [_btnStart setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btnStart addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnStart];
    
    
    _endImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ico_arrow"]];
    _endImage.frame = CGRectMake(gap + size.width/2, (size.height - imageSize.height)/2, imageSize.width, imageSize.height);
    [self addSubview:_endImage];
    
    _btnEnd = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnEnd.frame = CGRectMake(CGRectGetMaxX(_endImage.frame) - 25, 0, size.width/2 - imageSize.width, size.height);
       // _btnEnd.frame = CGRectMake(size.width/2, 0, size.width/2 , size.height);
//    _btnEnd.layer.borderWidth = 1;
//    _btnEnd.layer.borderColor = [UIColor grayColor].CGColor;
    [_btnEnd setTitle:@"设置终点" forState:UIControlStateNormal];
      _btnEnd.titleLabel.font = [UIFont systemFontOfSize:20];
    [_btnEnd setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_btnEnd addTarget:self action:@selector(endAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnEnd];
}

-(void)startAction{
    if (!_pinOverLayer && !_startPointOverLayer) {
        [self alertViewSendMessage:@"请在地图上选择一个起点"];
        return;
    }
    
    if (_pinOverLayer && !_startPointOverLayer) {
        UIImageView *tempView = (UIImageView *)_pinOverLayer.view;
        tempView.image = [UIImage imageNamed:@"定位起点"];
        _startPointOverLayer = _pinOverLayer;
        _pinOverLayer = nil;
        [_btnStart setTitle:@"重设起点" forState:UIControlStateNormal];
        _startImage.image = [UIImage imageNamed:@"ico_chongshe"];
        [self startNavitation];
    }else if (_startPointOverLayer){
        [_mapView removeOverlayer:_startPointOverLayer];
        _startPointOverLayer = nil;
       [_btnStart setTitle:@"设置起点" forState:UIControlStateNormal];
        _startImage.image = [UIImage imageNamed:@"ico_arrow"];
    }
    
    
}

-(void)endAction{
    if (!_pinOverLayer && !_endPointOverLayer) {
        [self alertViewSendMessage:@"请在地图上选择一个终点"];
        return;
    }
    
    if (_pinOverLayer && !_endPointOverLayer) {
        UIImageView *tempView = (UIImageView *)_pinOverLayer.view;
        tempView.image = [UIImage imageNamed:@"定位终点"];
        _endPointOverLayer = _pinOverLayer;
        _pinOverLayer = nil;
        [_btnEnd setTitle:@"重设终点" forState:UIControlStateNormal];
        _endImage.image = [UIImage imageNamed:@"ico_chongshe"];
        [self startNavitation];
    }else if (_endPointOverLayer){
        [_mapView removeOverlayer:_endPointOverLayer];
        _endPointOverLayer = nil;
        [_btnEnd setTitle:@"设为终点" forState:UIControlStateNormal];
        _endImage.image = [UIImage imageNamed:@"ico_arrow"];
    }

}

-(void)setTouchPoint:(CGPoint)Point{
    if (!_mapView || (_startPointOverLayer && _endPointOverLayer)) {
        return;
    }
    UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    tempImageView.center = Point;
    tempImageView.image = [UIImage imageNamed:@"定位"];
    
    if (!_pinOverLayer) {
        _pinOverLayer = [[NGROverlayer alloc]initWithView:tempImageView];
        [_mapView addOverlayer:_pinOverLayer];
        _pinOverLayer.floorId = _mapView.currentID;
        NSLog(@"%d",_mapView.currentID);
    }else{

        _pinOverLayer.worldPosition = [_mapView getWorldPositionFromScreenPosition:Point];
        [_mapView resetOverlayers];
    }
}

-(void)alertViewSendMessage:(NSString *)message{
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}



-(void)startNavitation{
    if (_startPointOverLayer && _endPointOverLayer) {
        self.btnStart.userInteractionEnabled = NO;
        self.btnEnd.userInteractionEnabled = NO;
        if (self.startNavigationBlock) {
            self.startNavigationBlock();
        }
    }else{
        return;
    }
}

-(void)removeSetStartAndEndPointView{

    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.frame;
        self.frame = CGRectMake(0, rect.origin.y + 64, rect.size.width, rect.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_mapView removeOverlayer:self.startPointOverLayer];
        [_mapView removeOverlayer:self.endPointOverLayer];

    }];
}

//直接修改worldPosition不会立即表现在视图上
-(void)changeLocationAboutStartAndEndPoint:(CGPoint)startP and:(CGPoint)endP{
        if (self.startPointOverLayer && self.endPointOverLayer && self.mapView) {
            self.startPointOverLayer.worldPosition = startP;
            self.endPointOverLayer.worldPosition = endP;
            [self.mapView resetOverlayers];
        }
}

#pragma mark- UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

}
@end
